
<template>
  <div class="page-box">
    <!-- 筛选区域 -->
    <div v-show="flag" class="search-box">
      <!-- :inline="true" 表单在一行显示 -->
      <el-form size="mini" :inline="true" :model="queryData" class="demo-form-inline">
        <el-form-item label="标题">
          <el-input v-model="queryData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="cateid" placeholder="请选择">
            <el-option
              v-for="ele in cateList"
              ref="select"
              :key="ele.id"
              :label="ele.catename"
              :value="ele.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="timestamp"
          />
          <!-- format="yyyy-MM-dd"
            value-format="yyyy-MM-dd" -->
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 工具条区域 -->
    <div class="tabbar-box">
      <el-row type="flex" justify="space-between">
        <div>
          <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="$router.push('/content_article/add')">新增</el-button>
          <el-button plain type="warning" icon="el-icon-download" size="mini">导出</el-button>
        </div>
        <div>
          <el-button v-if="flag" icon="el-icon-arrow-up" size="mini" plain circle @click="flag=!flag" />
          <el-button v-else icon="el-icon-arrow-down" size="mini" plain circle @click="flag=!flag" />
          <el-button icon="el-icon-refresh" size="mini" plain circle @click="initData()" />
          <el-button icon="el-icon-menu" size="mini" plain circle />
        </div>
      </el-row>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table
        :data="list"
        style="width: 100%"
        border
      >
        <el-table-column prop="id" label="ID" width="50" />
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="catename" label="分类名" />
        <el-table-column prop="pic" label="图片">
          <template #default="{row}">
            <img style="width:150px" :src="'http://124.223.14.236:8060/' + row.pic" alt="">
          </template>

        </el-table-column>
        <el-table-column prop="create_date" label="创建时间">
          <template #default="{row}">
            <span>{{ row.create_date | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ishot" label="是否推荐">
          <template #default="{row}">
            <el-switch
              v-if="row.isthot===1"
              v-model="val3"
              disabled
            />
            <el-switch
              v-else
              v-model="val4"
              disabled
            />
          </template>
        </el-table-column>

        <el-table-column prop="istop" label="是否置顶">
          <template #default="{row}">
            <el-switch
              v-show="row.istop === 1"
              v-model="val"
              disabled
            />
            <el-switch
              v-show="row.istop !== 1"
              v-model="val2"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="文章状态">
          <template #default="{row}">
            <el-tag v-if="row.status===2">待审核</el-tag>
            <el-tag v-else>已发布</el-tag>

          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template #default="{row}">
            <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="$router.push('/content_article/add')" />
            <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页区域 -->
    <div class="page">

      <el-pagination
        :current-page="queryData.page"
        :page-sizes="[1,2,5, 10, 20, 30]"
        :page-size="queryData.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

    </div>

  </div>
</template>
<script>
// import { formatDate } from '@/utils/common'
import { getArticleList, getCate, delArt } from '@/api/content/article'
export default {
  filters: { // 过滤器
    // 时间戳转日期
    formatDate: function(value) {
      const date = new Date(value)// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      const y = date.getFullYear()
      let MM = date.getMonth() + 1
      MM = MM < 10 ? ('0' + MM) : MM
      let d = date.getDate()
      d = d < 10 ? ('0' + d) : d
      // eslint-disable-next-line no-unused-vars
      let h = date.getHours()
      h = h < 10 ? ('0' + h) : h
      // eslint-disable-next-line no-unused-vars
      let m = date.getMinutes()
      m = m < 10 ? ('0' + m) : m
      // eslint-disable-next-line no-unused-vars
      let s = date.getSeconds()
      s = s < 10 ? ('0' + s) : s
      // return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;//多种时间格式的拼接
      return y + '-' + MM + '-' + d
    }
  },

  data() {
    return {
      flag: true,
      cateList: [],
      val3: true,
      val4: false,
      val: true,
      val2: false,
      list: [],
      cateid: '',

      time: '',
      queryData: {
        title: '',
        class: '',
        page: 1,
        limit: 10,
        start_time: '',
        end_time: '',
        cateid: ''
      },
      total: 0
    }
  },
  async created() {
    this.initData()
    const res = await getCate()
    // console.log(res)
    this.cateList = res.data
  },
  methods: {
    // 删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delArt({ id })
        if (this.list.length <= 1) {
          this.params.page -= 1
        }
        this.initData()
        // console.log(res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 重置
    reset() {
      this.initData()
    },
    // 搜索
    async search() {
      // this.queryData.page = 1
      // this.initData()
      const res = await getArticleList({
        start_time: this.time[0],
        end_time: this.time[1],
        title: this.queryData.title,
        cateid: this.cateid,
        page: 1,
        limit: 10
      })
      // console.log(res)
      this.list = res.data.data
      this.total = res.data.count
      // this.initData()
    },
    // 渲染表格
    async initData() {
      const res = await getArticleList(this.queryData)
      // console.log(res)
      this.list = res.data.data
      this.total = res.data.count
    },
    // 更改每一页的条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryData.limit = val
      this.queryData.page = 1
      this.initData()
    },
    // 更改页数
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.queryData.page = val
      this.initData()
    }
  }
}
</script>
<style lang="scss" scoped>
  .page-box {
    padding: 20px;
    .search-box,.tabbar-box {
      padding-top: 10px;
      border:  1px solid #eee;
      margin-bottom: 20px;
      height: 50px;
    }
  }

</style>
